package org.example.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.example.annotation.KafkaProcesser;
import org.example.dao.AppOrderMapper;
import org.example.model.po.AppOrder;
import org.example.service.AppOrderService;
import org.example.service.KafkaProcessHandler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 聚合支付平台-订单表(TAppOrder)表服务实现类
 *
 * @author makejava
 * @since 2020-05-06 10:15:03
 */
@Service("appOrderService")
@KafkaProcesser(key = "appOrder")
@Slf4j
public class AppOrderServiceImpl implements AppOrderService, KafkaProcessHandler {
    @Resource
    private AppOrderMapper appOrderMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param idOrderCtrl 主键
     * @return 实例对象
     */
    public AppOrder queryById(Long idOrderCtrl) {
        return this.appOrderMapper.queryById(idOrderCtrl);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    public List<AppOrder> queryAllByLimit(int offset, int limit) {
        return this.appOrderMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param appOrder 实例对象
     * @return 实例对象
     */
    @Transactional(rollbackFor = Exception.class)
    public AppOrder insert(AppOrder appOrder) {
        this.appOrderMapper.insert(appOrder);
        return appOrder;
    }

    /**
     * 修改数据
     *
     * @param appOrder 实例对象
     * @return 实例对象
     */
    @Transactional(rollbackFor = Exception.class)
    public AppOrder update(AppOrder appOrder) {
        this.appOrderMapper.update(appOrder);
        return this.queryById(appOrder.getIdOrderCtrl());
    }

    /**
     * 通过主键删除数据
     *
     * @param idOrderCtrl 主键
     * @return 是否成功
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long idOrderCtrl) {
        return this.appOrderMapper.deleteById(idOrderCtrl) > 0;
    }

    /**
     * @param message
     */
    public void handler(String message) {
        AppOrder appOrder = JSON.parseObject(message, AppOrder.class);
        appOrder.setNotifyStatus("0");
        appOrder.setOrderFlag("0");
        appOrder.setQuartetFlag("0");
        appOrder.setOrderInvaildTime("2020-05-06 15:45");
        appOrder.setCrtTime("2020-05-06 15:45");
        appOrder.setUpdTime("2020-05-06 15:45");
        appOrder = this.insert(appOrder);
        log.info("持久化后[appOrder]数据 \n {}", JSON.toJSONString(appOrder));
    }
}